#!/bin/bash

file_txt=$1

if [ ! -n "$1"  ];then
   echo "file list is null"
   exit -1
fi

IFS=$'\n' # default is space, tab, and return

sed -i 's/"//g' ${file_txt}
sed -i 's/\\/\//g' ${file_txt}
sed -i 's/\/\//\//g' ${file_txt}
#sed -i 's/ /\\ /g' ${file_txt}

mp3_dir="/tmp/mp3"
test ! -d $mp3_dir && mkdir -p $mp3_dir
mp4_dir="/tmp/mp4"
test ! -d $mp4_dir && mkdir -p $mp4_dir
start_seconds=$(date +%s)
echo '' > "/tmp/mp_file_names.txt"
for line in $(cat $file_txt); do
    echo "=================$line";
    start_this=$(date +%s)
    name_origin=$(echo "$line" | awk -F '@' '{print $1}')
    name_fullpath=$(echo "$line" | awk -F '@' '{print $2}')
    ls -alth "$name_fullpath"

    file_type=${name_fullpath##*.}
    if [ ! -n "$file_type"  ];then
    	echo "$name_fullpath 's file_type is null"
	continue
    fi

    name_uuid=$(cat /proc/sys/kernel/random/uuid | sed "s/-//g")
    if [[ "$file_type" = 'mp3'  ]]; then
        cp "$name_fullpath" "$mp3_dir/$name_uuid.mp3"
	    echo "$name_origin@$name_uuid.mp3" >> "/tmp/mp_file_names.txt"
    elif [[ "$file_type" = 'wav' ]]; then
        ffmpeg -i "$name_fullpath" -vn -ar 44100 -ac 2 -ab 128k -f mp3 "$mp3_dir/$name_uuid.mp3" &> /dev/null
        echo "$name_origin@$name_uuid.mp3" >> "/tmp/mp_file_names.txt"
    elif [[ "$file_type" = 'mp4' ]] || [[  "$file_type" = 'avi' ]] || [[ "$file_type" = 'mpg' ]]; then
	    ffmpeg -y -i "$name_fullpath" -vcodec libx264 -s 1280*720 -b 1000k -acodec aac -ab 128k -f mp4 "$mp4_dir/$name_uuid.mp4" &> /dev/null
        echo "$name_origin@$name_uuid.mp4" >> "/tmp/mp_file_names.txt"
    else
        echo "$name_fullpath"
    fi
    end_this=$(date +%s)
    echo "use $(echo $end_this $start_this | awk '{ print ($1 - $2)}')s."
    ls -alth "$file_type/$name_uuid.$file_type"
done
end_seconds=$(date +%s)
echo "total use $(echo $end_seconds $start_seconds | awk '{ print ($1 - $2)}')s."